package cn.org.timespace.ons.service.base.impl;

import java.util.Date;
import java.util.List;

import cn.org.timespace.ons.action.base.MomModel;
import cn.org.timespace.ons.action.policy.MomStatusModel;
import cn.org.timespace.ons.dao.base.MomDao;
import cn.org.timespace.ons.dao.policy.MomStatusDao;
import cn.org.timespace.ons.service.base.MomService;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.base.Mom;
import cn.org.timespace.ons.vo.base.Options;
import cn.org.timespace.ons.vo.policy.MomStatus;
public class MomServiceImpl implements MomService {

	private MomDao momDao;
	private MomStatusDao momStatusDao;
	
	public MomDao getMomDao() {
		return momDao;
	}

	public void setMomDao(MomDao momDao) {
		this.momDao = momDao;
	}
	

	public MomStatusDao getMomStatusDao() {
		return momStatusDao;
	}

	public void setMomStatusDao(MomStatusDao momStatusDao) {
		this.momStatusDao = momStatusDao;
	}

	@Override
	public void delete(Mom persistentInstance) {

		this.momDao.delete(persistentInstance);
	}

	@Override
	public List<Mom> findByCondition(MomModel model) {
		
		return this.momDao.findByCondition(model);
	}

	@Override
	public List<Object> findByCondition(MomModel model, Pager p) {
		
		return this.momDao.findByCondition(model, p);
	}

	@Override
	public Mom findById(String id) {
		
		
		return this.momDao.findById(id);
	}

	@Override
	public void save(Mom transientInstance) {

		this.momDao.save(transientInstance);
		
		MomStatus momStatus = new MomStatus();
		momStatus.setMomid(transientInstance.getMomid());
		momStatus.setSentTime(new Date());
		momStatus.setStatus("0");
		this.momStatusDao.save(momStatus);
	}

	@Override
	public void update(Mom instance) {
		
		this.momDao.update(instance);
		int status = instance.getStatus();
		if(status == 0){
			MomStatusModel model = new MomStatusModel();
			model.setMomid(instance.getMomid());
			MomStatus momStatus = momStatusDao.findByCondition(model).get(0);
			momStatus.setStatus("1");
			momStatusDao.update(momStatus);
		}
	}

	@Override
	public List<Mom> findCompCodeAndDeviceCode(String deviceCode,String compCode) {
		
		return this.momDao.findCompCodeAndDeviceCode(deviceCode,compCode);
	}

	@Override
	public List<Options> findByCode(String code) {
	
		return this.momDao.findByCode(code);
		
	}

	@Override
	public List findByName(String name) {
		
		return this.momDao.findByName(name);
	}

	@Override
	public List findByTheId(String theId) {
		
		return this.momDao.findByTheId(theId);
	}
}
